package com.youoor.dto.handler;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor;

import com.youoor.dto.RetDTO;




public class RetDTOHandler extends AbstractMessageConverterMethodProcessor{

	private static final int HTTP_STATUS_FAILED = 599;
	private static final int HTTP_STATUS_OK = 298;


	@SuppressWarnings("serial")
	public RetDTOHandler() {
		
		this(new ArrayList<HttpMessageConverter<?>>() {{ add(new MappingJackson2HttpMessageConverter()); }});

	}
	
	protected RetDTOHandler(List<HttpMessageConverter<?>> messageConverters) {
		super(messageConverters);
		
	}
	
	protected RetDTOHandler(List<HttpMessageConverter<?>> messageConverters,
			ContentNegotiationManager manager) {
		super(messageConverters, manager);
		
	}

	@Override
	public boolean supportsReturnType(MethodParameter returnType) {

		Class<?> type = returnType.getParameterType();  
        if(RetDTO.class.equals(type))  
        {  
            return true;  
        }  
        return false;  
	}

	@Override
	public void handleReturnValue(Object returnValue,MethodParameter returnType, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest) throws Exception {		 

			HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
			
			mavContainer.setRequestHandled(true);	
			
			Map<String,Object> model = new HashMap<String,Object>();
			if (returnValue != null) {				
				RetDTO resultDTO = (RetDTO)returnValue;				
				int status = HTTP_STATUS_OK;
				
				if(!resultDTO.isSuccess()) {
					status = HTTP_STATUS_FAILED;
				}
				Map<String,Object> result = resultDTO.getResult();
				for(String key : result.keySet()) {
					model.put(key, result.get(key));
				}
				System.out.println(status);
				response.setStatus(status);
				webRequest.setAttribute("response_status", status, RequestAttributes.SCOPE_REQUEST);
				webRequest.setAttribute("returnValue", model, RequestAttributes.SCOPE_REQUEST);
				
				if(model != null)
					writeWithMessageConverters(model,returnType,webRequest);
			}
	}

	protected ServletServerHttpResponse createOutputMessage(NativeWebRequest webRequest) {
		HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
		return new ServletServerHttpResponse(response);
	}
	
	protected ServletServerHttpRequest createInputMessage(NativeWebRequest webRequest) {
		HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
		return new ServletServerHttpRequest(servletRequest);
	}
	
	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return false;
	}

	@Override
	public Object resolveArgument(MethodParameter parameter,
			ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
			WebDataBinderFactory binderFactory) throws Exception {
		
		return null;
	}
	

}

